/*
 * PcmPlayoutBuffer.cpp
 *
 *  Created on: Nov 27, 2011
 *      Author: shaohong
 */

#include "PcmPlayoutBuffer.h"
#include "PCM_Data_Block.h"
#include "NmpsException.h"
#include "AppLogger.h"
#include <stdio.h>

static AppLogger * logger = AppLogger::getInstance("PcmPlayoutBuffer");

namespace nmps {


PcmPlayoutBuffer::PcmPlayoutBuffer() {
}

PcmPlayoutBuffer::~PcmPlayoutBuffer() {
}


void PcmPlayoutBuffer::clearAllData() {
	while (!q.empty()) {
		PCM_Data_Block * pcmDataBlock = q.front();
		delete pcmDataBlock;
		q.pop_front();
	}
}

void PcmPlayoutBuffer::enQueuePcmDataBlock(int blockSize, unsigned char * pcmData) {
	if (blockSize <= 0){
		throw NmpsException("blockSize <= 0");
	}

//	if (blockSize <= 160) {
//		// skip this for now
//		return;
//	}

	PCM_Data_Block * pcmDataBlock = new PCM_Data_Block(blockSize);
	pcmDataBlock->setPcmData(pcmData);

//	logger->debug("before appending q.Size() is %d\n", q.size());
	q.push_back(pcmDataBlock);

//	logger->debug("appended PCMA block of size: %d bytes. q.size()=%d, q.max_size()=%d\n", blockSize, q.size(), q.max_size());
}

PCM_Data_Block * PcmPlayoutBuffer::deQueuePcmDataBlock()
{
	if (q.empty())
		return NULL;

	PCM_Data_Block * ret = q.front();

 	q.pop_front();
//	fprintf(stderr, "dequed ref: size = %d\n", ref->getDataSize());

// 	PCM_Data_Block * retVal = ret;
	return ret;
}

int PcmPlayoutBuffer::getNumberOfBlocks() {
	return q.size();
}

unsigned long PcmPlayoutBuffer::getTotalPCMDataSize() {
	list<PCM_Data_Block *>::iterator iter = q.begin();

	unsigned long audioDataSize = 0;
	while (iter != q.end()){
		audioDataSize += (*iter)->getDataSize();
		iter ++;
	}

	return audioDataSize;
}

} /* namespace nmps */
